package com.liuxing.javaSpringBoot.config;

import com.liuxing.javaSpringBoot.interceptor.ViewMappingInterceptor;
import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Created by IntelliJ IDEA.
 * @File WebMvcConfig.java
 * @Auth liuxing
 * @Date 2021/8/18 15:19
 * @Email liuxing997@foxmail.com
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Value("${server.http.port}")
    private int httpPort;

    @Autowired
    private ResourcesConfigerBean resourcesConfigerBean;

    @Autowired
    private ViewMappingInterceptor viewMappingInterceptor;

    //拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(viewMappingInterceptor).addPathPatterns("/**");
    }

    @Bean
    public Connector connector() {
        Connector connector = new Connector();
        connector.setPort(httpPort);
        connector.setScheme("http");
        return connector;
    }

    @Bean
    public ServletWebServerFactory servletWebServerFactory() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.addAdditionalTomcatConnectors(connector());
        return factory;
    }

    /*自定义静态资源目录
     * servlet：*和** 都匹配所有
     * spring：* 匹配一级路径 ** 匹配多级路径
     * */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //根据操作系统类型配置不同的路径
        String systemName = System.getProperty("os.name");
        if (systemName.toLowerCase().startsWith("win")) {
            registry.addResourceHandler(resourcesConfigerBean.getPathPattern())
                    .addResourceLocations(ResourceUtils.FILE_URL_PREFIX
                            + resourcesConfigerBean.getWindowsLocation());
        } else {
            registry.addResourceHandler(resourcesConfigerBean.getPathPattern())
                    .addResourceLocations(ResourceUtils.FILE_URL_PREFIX +
                            resourcesConfigerBean.getLinuxLocation());
        }

     /* //手动设置路径
      registry.addResourceHandler("/upload/**")
     * .addResourceLocations(ResourceUtils.FILE_URL_PREFIX + "/Users/liuxing/Downloads/upload/");
     */
    }

    /*
     * 跨域配置
     */
//    @Override
//    public void addCorsMappings(CorsRegistry registry){
//        // spring boot 42.4.2
//        registry.addMapping("/**")
//                .allowedOrigins("*")
//                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETEl")
//                .maxAge(3600)
//                .allowedOriginPatterns("*")
//                .allowCredentials(true);
//    }
}
